import com.mysql.jdbc.Connection;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SelectBook {
    public static void main(String[] args) throws SQLException {
        MysqlDataSource dataSource=new MysqlDataSource();
        dataSource.setURL("jdbc:mysql://127.0.0.1:3306/java33?characterEncoding=utf8&useSSL=true");
        dataSource.setUser("root");
        dataSource.setPassword("12345678");

        Connection connection= (Connection) dataSource.getConnection();

        PreparedStatement statement=connection.prepareStatement("select * from booklist where id<?");
        statement.setInt(1,6);

        ResultSet resultSet=statement.executeQuery();
        while (resultSet.next()){
            int id=resultSet.getInt("id");
            String name=resultSet.getString("name");
            String author=resultSet.getString("author");
            BigDecimal price=resultSet.getBigDecimal("price");
            String type=resultSet.getString("type");
            System.out.println(String.format("id: %s,name: %s,author: %s,price: %s,type: %s",id,name,author,price,type));
        }
        resultSet.close();
        statement.close();
        connection.close();
    }
}
